package com.base.ui

import android.content.DialogInterface
import android.os.Bundle
import android.text.TextUtils
import android.view.Menu
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import com.base.R
import com.base.constant.Extra
import com.base.exp.stringExtra
import com.base.util.PlayHelper
import com.loyal.core.dialog.LoadingDialog
import com.loyal.core.ui.activity.BasicActivity

abstract class BaseActivity : BasicActivity(), DialogInterface.OnCancelListener {
    private var loadingDialog: LoadingDialog? = null

    override fun setContentView(layoutResID: Int) {
        enableEdgeToEdge()
        super.setContentView(layoutResID)
        //binding.root.applyWindowInsets()

        initToolbar()
    }

    override fun setContentView(view: View?) {
        enableEdgeToEdge()
        super.setContentView(view)
        initToolbar()
    }

    private fun initToolbar() {
        val actionBar = supportActionBar
        actionBar?.setDisplayHomeAsUpEnabled(BACK_ENABLE)
        actionBar?.setHomeButtonEnabled(BACK_ENABLE)
        val newTitle = intent.stringExtra(Extra.TITLE)
        if (!TextUtils.isEmpty(newTitle))
            title = newTitle
    }

    override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)
        if (isFullScreen) {
            val controller = WindowCompat.getInsetsController(window, window.decorView)
            // 同时隐藏状态栏和导航栏
            controller.hide(WindowInsetsCompat.Type.systemBars())
            supportActionBar?.hide()
        }
    }

    @JvmOverloads
    fun showLoading(
        message: String?, outsideFlag: Boolean = false,
        cancelFlag: Boolean = true, listener: DialogInterface.OnCancelListener? = null
    ) {
        dismissLoading()
        if (null == loadingDialog) loadingDialog = LoadingDialog(this)
        loadingDialog?.let {
            it.setCanceledOnTouchOutside(outsideFlag)
            it.setCancelable(cancelFlag)
            it.setMessage(replaceText(message))
            it.setOnCancelListener(listener ?: this)
            it.show()
        }
    }

    fun dismissLoading() {
        if (null != loadingDialog) loadingDialog!!.dismiss()
    }

    /**
     * 在需要设置禁止截屏的Activity的onCreate()方法中调用setContentView()方法前添加一行代码即可：
     * getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
     * 或：
     * getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
     */
    override fun setLayoutOrView() {
        val layoutRes = actLayoutRes()
        if (layoutRes > 0)
            super.setLayoutOrView()
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.screen, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun finish() {
        super.finish()
        PlayHelper.getInstance().release()
    }

    override fun onPause() {
        dismissLoading()
        super.onPause()
    }

    override fun onCancel(dialog: DialogInterface?) {
    }

    override fun onDestroy() {
        if (null != loadingDialog) loadingDialog = null
        super.onDestroy()
    }

    companion object {
        private const val BACK_ENABLE = false
    }
}